﻿using Bll.User;
using ShareCode.Entity.User;
using Infrastructure.Attributes.Ioc;
using Microsoft.AspNetCore.Mvc;
using Infrastructure.Model;
using Infrastructure.Util.Helper;
using ShareCode.Dto;
using Infrastructure.MyException;
using ShareCode.Dto.User.Org.Admin;

namespace ApiApp.Controllers.User
{
    /// <summary>
    /// by zhouxin
    ///</summary>
    [ApiController]
    [Route("User/[controller]")]
    [Component(ServiceLifetime.Transient)]
    public class UserOrgController : BaseTreeController<UserOrgEntity>
    {
        protected override UserOrgBll? Bll => (UserOrgBll?)base.Bll;

        [Autowired]
        public UserOrgBll SetBll { set { this._Bll = value; } }


        [HttpPost("AdminNew")]
        public Task<JsonResult> AdminNewAsync([FromForm] FileUploadDto<NewOrgDto> entity)
        {
            entity.Model!.RequestToken = entity.Model.RequestToken ?? "";
            var myr = new ShowUserLogicException("注册信息验证不通过");
            if (string.IsNullOrEmpty(entity.Model.Verification)) { myr.AddSonMessage("Verification", "验证码无效"); }
            else if (!(RedisHelper.GetObject(RedisDbBusiness.Core)?.KeyExists(RedisKeyPrefix.ValidCode + entity.Model.RequestToken + entity.Model.Verification.ToLower())??false))
            {
                myr.AddSonMessage("Verification", "验证码无效");
            }

            if (myr.SonErrs.Count > 0)
            {
                return Task.FromResult(Err(myr));
            }

            return OkkAsync(Bll!.AdminNewAsync(entity));
        }


        [HttpPost("AdminList")]
        public async virtual Task<JsonResult> AdminList([FromBody] PagingDto? dto = null)
        {
            return await OkkAsync(Bll!.AdminListAsync(dto));
        }

    }
}
